/*
package com.manger.utils;

import com.alibaba.fastjson.JSONObject;
import com.benjaminwan.ocrlibrary.OcrResult;
import com.benjaminwan.ocrlibrary.TextBlock;
import com.manger.model.UserInfo;
import io.github.mymonstercat.Model;
import io.github.mymonstercat.ocr.InferenceEngine;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class OCRUtils {


    private static List<String> suffixList = new ArrayList<String>();

   static  {
        suffixList.add(":");
        suffixList.add("：");
        suffixList.add("-");
        suffixList.add("\n");
    }

    */
/**
     * 使用OCR技术处理MultipartFile对象并返回识别的文本
     * 该方法首先将MultipartFile对象保存到临时文件，然后使用OCR引擎识别文件中的文本
     *
     * @param multipartFile 要进行OCR处理的MultipartFile对象
     * @return 识别到的文本字符串
     * @throws RuntimeException 如果文件传输或OCR处理过程中发生错误
     *//*

    public static String bYMultipartFileOcr(){
        // 构造临时文件路径
        String  url = "C:\\Users\\liuhj\\Desktop\\2025-07-18_144919.png";
        File file = new File(url);
        OcrResult ocrResult = null;
        try {
            // 将MultipartFile内容转移到临时文件
            // 获取OCR引擎实例
            InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);

            // 使用OCR引擎处理临时文件并获取结果
            ocrResult = engine.runOcr(url);

            // 打印识别结果

            // 返回识别结果
            return ocrResult.getStrRes().trim();
        } catch (Exception e) {
            // 如果发生错误，抛出运行时异常
            throw new RuntimeException(e);
        } finally {
            // 确保临时文件被删除
            if (file.exists() && !file.delete()) {
                // log.warn("临时文件删除失败: {}", file.getAbsolutePath());
            }
        }
    }

    */
/**
     * 使用OCR技术处理给定路径的图像文件并返回识别的文本
     *
     * @param path 图像文件的路径
     * @return 识别到的文本字符串
     * @throws RuntimeException 如果OCR处理过程中发生错误
     *//*

    public static UserInfo bYPathOcr(String path){
        OcrResult ocrResult = null;
        try {
            // 获取OCR引擎实例
            InferenceEngine engine = InferenceEngine.getInstance(Model.ONNX_PPOCR_V3);
            // 使用OCR引擎处理给定路径的文件并获取结果
            ocrResult = engine.runOcr(path);
            String content = format(ocrResult.getStrRes().trim());
            List<TextBlock> list = ocrResult.getTextBlocks();
          */
/*  UserInfo userInfo = new UserInfo();
            for(int i = list.size() - 1; i >= 0 ; i--){
                TextBlock textBlock = list.get(i);
                String  text =  textBlock.getText();
                if(text.contains("公民")){
                    userInfo.setIdCard(findStrByRegex(text,"\\d{17}[0-9Xx]"));
                    list.remove(i);
                }
                if(text.contains("性别")){
                    userInfo.setSex(text.replaceAll("性别",""));
                    list.remove(i);
                }
                if(text.contains("姓名")){
                    userInfo.setUserName(text.replaceAll("姓名",""));
                    list.remove(i);
                }
                if(text.contains("出生")){
                    list.remove(i);
                }
                if(text.contains("民族")){
                    userInfo.setNation(text.replaceAll("民族",""));
                    list.remove(i);
                }
            }
            if(userInfo.getIdCard() != null && !"".contains(userInfo.getIdCard().trim())){
                userInfo.setBirthday(userInfo.getIdCard().trim().substring(6,14));
            }

            String address = "";

            for(TextBlock textBlock : list){
                address += textBlock.getText();
            }
            if(address.length() > 3){
                address = address.substring(2,address.length());
            }

            userInfo.setAddress(address);*//*


            UserInfo userInfo = new UserInfo();
            userInfo.setIdCard(findStrByRegex(content,"\\d{17}[0-9Xx]"));
            if(userInfo.getIdCard() != null && !"".contains(userInfo.getIdCard().trim())){
                userInfo.setBirthday(userInfo.getIdCard().trim().substring(6,14));
            }
            userInfo.setAddress(findStrByRegex(content,"住址","公民"));
            if(userInfo.getAddress() == null || "".contains(userInfo.getAddress().trim())){
                userInfo.setAddress(findStrByRegex(content,"往址","公民"));
            }
            if(userInfo.getAddress() == null || "".contains(userInfo.getAddress().trim())){
                userInfo.setAddress(findStrByRegex(content,"址","公民"));
            }
            userInfo.setNation(findStrByRegex("性别","出生"));
            userInfo.setUserName(findStrByRegex(content,"姓名","性别"));
            userInfo.setSex(findStrByRegex(content,"性别","民族"));
            return userInfo;

        } catch (Exception e) {
            // 如果发生错误，抛出运行时异常
            throw new RuntimeException(e);
        }
    }

    private static String format(String content){
        for(String s : suffixList){
            content  = content.replaceAll(s, "");
        }
        return content;
    }

    private static String findStrByRegex(String content, String regex){
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            return matcher.group();
        }
        return "";
    }

    private static String findStrByRegex(String content,String start,String end){
        Pattern pattern = Pattern.compile(start + "(.*?)" + end);
        Matcher matcher = pattern.matcher(content);
        while (matcher.find()) {
            String temp = matcher.group();
            temp = temp.replaceAll(start, "").replaceAll(end, "");
            return temp;
        }
        return "";
    }

    public static void main(String[] args) {
       UserInfo userInfo =  OCRUtils.bYPathOcr("C:\\Users\\liuhj\\Desktop\\新建文件夹 (2)\\1.jpg");
        System.out.println(JSONObject.toJSON(userInfo));
    }
}
*/
